<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>发送 AJAX 请求</title>
  </head>

  <body>
    <script>
      function sendAJAX(url) {
        return new Promise((resolve, reject) => {
          const xhr = new XMLHttpRequest()
          xhr.open('GET', url)
          xhr.send()
          xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
              if (xhr.status < 300 && xhr.status >= 200) {
                // 用JSON.parse处理响应的字符串数据，结果为对象
                let json = JSON.parse(xhr.responseText)
                resolve(json)
              }
              reject(xhr.status)
            }
          }
        })
      }
      async function main() {
        const res = await sendAJAX('https://api.apiopen.top/getJoke')
        const res1 = await sendAJAX('http://www.tianqiapi.com/api?version=v1&appid=23035354&appsecret=8YvlPNrz')
        console.log(res.result)
        console.log(res1)
      }
      main()
    </script>
  </body>
</html>
